package com.itliuhouse.success.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itliuhouse.success.domain.dto.PageDTO;
import com.itliuhouse.success.domain.dto.UserFormDTO;
import com.itliuhouse.success.domain.query.UserQuery;
import com.itliuhouse.success.domain.vo.UserVO;
import com.itliuhouse.success.pojo.User;
import com.itliuhouse.success.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("users")
public class UserController {
    //初始化的时候就会进行实例化注入
    private final UserService userService;

    @PostMapping
    @ApiOperation("新增用户")
    public void saveUser(@RequestBody UserFormDTO userFormDTO){
        System.out.println(userFormDTO);
        //1.转化DTO为pojo,两个之间的属性要一样才能赋值成功
        User user = BeanUtil.copyProperties(userFormDTO , User.class);
        //2.新增
        userService.save(user);
    }


    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable("id") Long id){
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户接口")
    @GetMapping("useraddress/{id}")
    public UserVO queryUserById(@ApiParam("用户ID") @PathVariable("id") Long id){
        //1.查询用户
        User user = userService.getById(id);
        //2.处理vo
        return BeanUtil.copyProperties(user , UserVO.class);
    }

    @ApiOperation("根据id查询用户信息和地址信息")
    @GetMapping("{id}")
    public UserVO queryUserAndAddressById(@ApiParam("用户ID") @PathVariable("id") Integer id){
        //基于自定义的service方法进行查询
        return userService.queryUserAndAddressById(id);
    }



    @GetMapping
    @ApiOperation("根据id集合查询用户")
    public List<UserVO> queryUserByIds(@RequestParam("ids") List<Long> ids){
        //1.查询用户
        List<User> users = userService.listByIds(ids);
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        //2.处理vo
        return userVOS;
    }


    @GetMapping("userall")
    @ApiOperation("根据id批量查询用户接口")
    public List<UserVO> queryUserAllByIds(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids){
        return userService.queryUserAndAddressByIds(ids);
    }


    @PutMapping("{id}/deduction/{money}")
    @ApiOperation("扣减用户余额")
    public void deductBalance(@PathVariable("id") Long id , @PathVariable("money") Integer money){
        userService.deductBalance(id , money);
    }


    @GetMapping("/list")
    @ApiOperation("根据id集合查询用户")
    public List<UserVO> queryUsers(UserQuery query){
        //1.组织条件
        String username = query.getName();
        Integer status = query.getStatus();
        Integer minBalance = query.getMinBalance();
        Integer maxBalance = query.getMaxBalance();
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda()
                .like(username != null, User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance);
        //2.查询用户
        List<User> user_list = userService.list(wrapper);
        //3.处理vo
        return BeanUtil.copyToList(user_list , UserVO.class);
    }

    @GetMapping("/listlambda")
    @ApiOperation("根据id集合查询用户lambda")
    public List<UserVO> queryUsersByLambda(UserQuery query){
        //1组织条件
        String username = query.getName();
        Integer status = query.getStatus();
        Integer minBalance = query.getMinBalance();
        Integer maxBalance = query.getMaxBalance();
        //2.查询用户
        List<User> user_list = userService.lambdaQuery()
                .like(username != null, User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();
        return BeanUtil.copyToList(user_list , UserVO.class);
    }


    /**
     * 获取用户的分页数据
     * 传递过来的参数会被 UserQuery对象封装
     * @return
     */
    @GetMapping("/page")
    public PageDTO<UserVO> queryUsersPage(UserQuery query){
        return userService.queryUsersPage(query);
    }

    @GetMapping("/page_simple")
    @ApiOperation("自定义方法实现数据转换")
    public PageDTO<UserVO> queryUsersByPage(UserQuery query){
        return userService.queryUsersByPage(query);
    }



}
